﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Reflection;
using System.Data.SqlClient;

namespace JJY.DBUtility
{
    public class SQLEntityHelper<T> : SQLHelper where T : class
    {
        protected static T GetEntity(string conString, string sql, params SqlParameter[] commandParameters)
        {
            T result = null;
            List<T> tSet = GetEntitySet(conString, sql, commandParameters);
            if(tSet.Count> 0)
            {
                result = tSet.FirstOrDefault();
            }
            return result;
        }

        protected static List<T> GetEntitySet(string conString, CommandType commandType, string sql, params SqlParameter[] commandParameters)
        {
            return GetEntitySet(SQLHelper.ExecuteReader(conString, commandType, sql, commandParameters));
        }

        protected static List<T> GetEntitySet(string conString, string sql, params SqlParameter[] commandParameters)
        {
            return GetEntitySet(SQLHelper.ExecuteReader(conString, CommandType.Text, sql, commandParameters));
        }

        protected static List<T> GetEntitySet(SqlDataReader dr)
        {
            List<T> result = new List<T>();

            using (dr)
            {
                DataView tempView = dr.GetSchemaTable().DefaultView;
                while (dr.Read())
                {
                    T temp = Assembly.Load(Assembly.GetAssembly(typeof(T)).FullName).CreateInstance(typeof(T).ToString()) as T;
                    foreach (PropertyInfo propertyInfo in typeof(T).GetProperties())
                    {
                        tempView.RowFilter = "ColumnName = '" + propertyInfo.Name + "'";
                        if (tempView.Count > 0 &&
                            propertyInfo.PropertyType == dr[propertyInfo.Name].GetType())
                        {
                            propertyInfo.SetValue(temp, dr[propertyInfo.Name], null);
                        }
                    }
                    result.Add(temp);
                }
            }

            return result;
        }

        protected static List<T> GetEntitySet(DataTable dataTable)
        {
            List<T> result = new List<T>();

            foreach (DataRow dataRow in dataTable.Rows)
            {
                T temp = Assembly.Load(Assembly.GetAssembly(typeof(T)).FullName).CreateInstance(typeof(T).ToString()) as T;
                foreach (PropertyInfo propertyInfo in typeof(T).GetProperties())
                {
                    if (dataTable.Columns.Contains(propertyInfo.Name) &&
                        dataTable.Columns[propertyInfo.Name].DataType == propertyInfo.PropertyType &&
                        !(dataRow[propertyInfo.Name] is DBNull))
                    {
                        propertyInfo.SetValue(temp, dataRow[propertyInfo.Name], null);
                    }
                }
                result.Add(temp);
            }

            return result;
        }

        protected static bool BulkInsert()
        {
            bool result = false;
            return result;
        }
    }
}
